package com.telecom.cms.util;

import java.util.*;
import java.math.*;
import java.sql.*;
import java.text.*;
import com.telecom.cms.dao.*;

 
 
public class WebTools {


/**
 * ���������ƺʹ���ת������
 * @param sqlStr SQL ��䣬��һ���ֶ�Ϊ�����ֶΣ��ڶ���Ϊ����ֵ�ֶΡ�
 * @param value Ҫ�����ֵ
 * @return �ʹ����Ӧ�����
 */
	public static String getNameByValue(String sqlStr,String value) {
		String result = "";
		Connection conn=null;
		Statement stm=null;
		ResultSet rs=null;
		
		try {
			conn=GetConnect.getCsConnection();
			stm=conn.createStatement();
			rs=stm.executeQuery(sqlStr);
			while(rs.next()){
				//System.out.println("string:"+value);
				if(rs.getString(1).trim().equals(value.trim())){
					result=rs.getString(2);
					break;
				}
			}
								
		} catch (Exception ex) {
			ex.printStackTrace();
		}finally{
			try{
			
			rs.close();
			stm.close();

			
			}catch(Exception ex1){
				ex1.printStackTrace();
			}
		}
		
		return result;
	}
	
	public static String show(Object data){
		if(data==null || data.equals("null")){
			return "&nbsp;";
		}else{
			//System.err.println("showdata:"+data.toString());
			return ""+data;
		}
	}
	
	public static String showEx(Object data){
		if(data==null || data.equals("null")){
			return "";
		}else{
			return ""+data;
		}
	}
	
	
public static String getTwoDay(String date1, String date2)
	 {

		String time1 = date1;
		String time2 = date2;
		String date_tem="";
		Calendar calendar1 = new GregorianCalendar();
		Calendar calendar2 = new GregorianCalendar();
		Calendar temp = new GregorianCalendar();
		SimpleDateFormat  form=new SimpleDateFormat("yyyy-MM-dd");
		//Date trialTime = new Date();
		//Date  d=new Date();
		
		
		
	StringTokenizer st=new StringTokenizer(time1,"-");		
	calendar1.set(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken())-1,Integer.parseInt(st.nextToken()));
	st=new StringTokenizer(time2,"-");
	calendar2.set(Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken())-1,Integer.parseInt(st.nextToken()));

		while(calendar1.before(calendar2) && (!calendar1.equals(calendar2)))
		{
		//System.out.println(form.format(calendar1.getTime()));
		calendar1.add(Calendar.DATE,1);
			if(!calendar1.equals(calendar2))
			{
			date_tem=date_tem+"'"+form.format(calendar1.getTime())+"',";
			}else
			{
			date_tem=date_tem+"'"+form.format(calendar1.getTime())+"'";
			}
		}
		
		System.out.println("���ڶ�Ϊ:"+date_tem);
		return  date_tem;
	}
	
	//��-�б�
	public static String getTwoSelect(String parName,String parSql)
{
		StringBuffer buffer=new StringBuffer("");
		Connection conn=null;
		Statement stm=null;
		ResultSet rs=null;
		try{
			buffer.append("<SELECT name='"+parName+"' onChange='ChangeLocation(RoleArray,entityForm.parName[entityForm.parName.selectedIndex].value,entityForm.subName)'>");
			conn=GetConnect.getCsConnection();
			stm=conn.createStatement();
			rs=stm.executeQuery(parSql);

			buffer.append("<OPTION VALUE=\"\" SELECTED>--��ѡ��--</OPTION>\n");	
			while(rs.next())
			{
				buffer.append("<OPTION VALUE=\"" + rs.getObject(2) + "\">" + rs.getObject(1) + "</OPTION>\n");				
			}
			
			buffer.append("</SELECT>");
						
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			try{

				rs.close();
				stm.close();
				rs=null;
				stm=null;
				
			}catch(Exception e){
			}	
		}
		
		return buffer.toString();	
	}	

	//��-�б�
	public static String getTwoSelectOther(String parName,String parSql)
{
		StringBuffer buffer=new StringBuffer("");
		Connection conn=null;
		Statement stm=null;
		ResultSet rs=null;
		try{
			buffer.append("<SELECT name='"+parName+"' onChange='ChangeLocation(RoleArray,entityForm.CampName[entityForm.CampName.selectedIndex].value,entityForm.CampSeqName)'>");
			conn=GetConnect.getCsConnection();
			stm=conn.createStatement();
			rs=stm.executeQuery(parSql);

		 buffer.append("<OPTION VALUE=\"\" SELECTED>--��ѡ��--</OPTION>\n");
			while(rs.next())
			{
				buffer.append("<OPTION VALUE=\"" + rs.getObject(2) + "\">" + rs.getObject(1) + "</OPTION>\n");				
			}
			
			buffer.append("</SELECT>");
						
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			try{
			
				rs.close();
				stm.close();
				rs=null;
				stm=null;
				
			}catch(Exception e){
			}	
		}
		
		return buffer.toString();	
	}	
	
		public static String getTwoSelectOther2(String parName,String parSql)
{
		StringBuffer buffer=new StringBuffer("");
		Connection conn=null;
		Statement stm=null;
		ResultSet rs=null;
		try{
			buffer.append("<SELECT name='"+parName+"'   >");
			conn=GetConnect.getCsConnection();
			stm=conn.createStatement();
			rs=stm.executeQuery(parSql);
buffer.append("<OPTION VALUE=\"\" SELECTED>--��ѡ��--</OPTION>\n");

		 
			while(rs.next())
			{        
                                
				buffer.append("<OPTION VALUE=\"" + rs.getObject(2) + "\">" + rs.getObject(1) + "</OPTION>\n");				
			}
			
			buffer.append("</SELECT>");
 
                       
						
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			try{
				
				rs.close();
				stm.close();
				rs=null;
				stm=null;
				
			}catch(Exception e){
			}	
		}
		
		return buffer.toString();	
	}	
	
		//��-�б�
	public static String createTree()
{
	StringBuffer buffer=new StringBuffer("");
			ArrayList arraylist1 = new ArrayList();
			ArrayList arraylist2 = new ArrayList();
			ArrayList arraylist3 = new ArrayList();
			ArrayList arraylist4 = new ArrayList();
			ArrayList arraylist5 = new ArrayList();
			ArrayList arraylist = new ArrayList();
			
			arraylist1.add("�����Ʒ");
			arraylist1.add("tree50032");
			arraylist1.add("-1");
			
			arraylist2.add("�̶��绰");
			arraylist2.add("tree50079");
			arraylist2.add("tree50032");
			
			arraylist3.add("С��ͨ");
			arraylist3.add("tree50080");
			arraylist3.add("tree50032");
			
			arraylist4.add("�����Ʒ");
			arraylist4.add("tree50033");
			arraylist4.add("-1");
			
			arraylist5.add("�Զ����Ʒ��Ʒ");
			arraylist5.add("tree50034");
			arraylist5.add("tree50033");
			
			arraylist.add(arraylist1);
			arraylist.add(arraylist2);
			arraylist.add(arraylist3);
			arraylist.add(arraylist4);
			arraylist.add(arraylist5);
			
			//String[][] arg= new String{{"�����Ʒ","tree50032","-1"},{"�̶��绰","tree50079","tree50032"},{"С��ͨ","tree50080","tree50032"},{"�����Ʒ","tree50033","-1"}};
			/*String[][] arg = {
				{"�����Ʒ","tree50032","-1"},
				{"�̶��绰","tree50079","tree50032"},
				{"С��ͨ","tree50080","tree50032"},
				{"�����Ʒ","tree50033","-1"}};*/
			
			 
	buffer.append("<SCRIPT LANGUAGE=javascript>");
	buffer.append("var tree = new WebFXTree('��Ʒ','','','TreeImage/Icon_FClose.gif','TreeImage/Icon_BoRep.gif');");
	/*for(int i=0;i<arraylist.size();i++)
	{
		if(((ArrayList)arraylist.get(i)).get(2).toString().equals("-1"))
		{
		 buffer.append("var "+ ((ArrayList)arraylist.get(i)).get(1).toString() + " = new WebFXTreeItem(' "+((ArrayList)arraylist.get(i)).get(0).toString()+" ','','','TreeImage/Icon_FClose.gif','TreeImage/Icon_FOpen.gif','50032','50032',0);");
	     buffer.append("tree.add("+((ArrayList)arraylist.get(i)).get(1).toString()+");");
		}
	}
	*/
	int flag = 1;
	ArrayList templist = new ArrayList();
	ArrayList templist1 = new ArrayList();
	templist1.add("");
	templist1.add("-1");
	templist1.add("");
	templist.add(templist1);

	ArrayList templist2 = new ArrayList();
	int count;

	while(flag == 1)
	{
		flag = 0;
		
		count = arraylist.size();
		
		for(int i=0;i < count;i++)
		{
		
			for(int j=0;j<templist.size();j++)
			{
			if(((ArrayList)arraylist.get(i)).get(2).toString().equals(((ArrayList)templist.get(j)).get(1).toString()))
			{
				if(((ArrayList)templist.get(j)).get(1).toString().equals("-1"))
				{   
					buffer.append("var "+ ((ArrayList)arraylist.get(i)).get(1).toString() + " = new WebFXTreeItem(' "+((ArrayList)arraylist.get(i)).get(0).toString()+" ','','','TreeImage/Icon_FClose.gif','TreeImage/Icon_FOpen.gif','50032','50032',0);");
					buffer.append("tree.add("+((ArrayList)arraylist.get(i)).get(1).toString()+");");
					
				}
				else
				{
					buffer.append("var "+ ((ArrayList)arraylist.get(i)).get(1).toString() + " = new WebFXTreeItem(' "+((ArrayList)arraylist.get(i)).get(0).toString()+" ','','','TreeImage/Icon_FClose.gif','TreeImage/Icon_FOpen.gif','50032','50032',0);");
				    buffer.append(((ArrayList)templist.get(j)).get(1).toString()+".add("+((ArrayList)arraylist.get(i)).get(1).toString()+");");
				}
				flag = 1;
				templist2.add((ArrayList)arraylist.get(i));
				
			}
			}
		}
		
		for(int i=0;i<templist2.size();i++)
		{
			arraylist.remove((ArrayList)templist2.get(i));
		}
		templist.clear();
		templist =  	(ArrayList)templist2.clone() ;
		templist2.clear();
	
		
	}

	 

	 

//	buffer.append("tree.setBehavior('classic');");
	 
//	buffer.append("var tree50032 = new WebFXTreeItem('�����Ʒ','','','TreeImage/Icon_FClose.gif','TreeImage/Icon_FOpen.gif','50032','50032',0);");
//	buffer.append("tree.add(tree50032);");
	 
//	buffer.append("var tree50079 = new WebFXTreeItem('�̶��绰','','','TreeImage/DisIcon_FClose.gif','TreeImage/DisIcon_FOpen.gif','50079','50079',0);");
//	buffer.append("tree50032.add(tree50079);");
	 
//	buffer.append("var tree50080 = new WebFXTreeItem('С��ͨ','','','TreeImage/DisIcon_FClose.gif','TreeImage/DisIcon_FOpen.gif','50080','50080',0);");
//	buffer.append("tree50032.add(tree50080);");
	 
//	buffer.append("var tree50033 = new WebFXTreeItem('�����Ʒ','','','TreeImage/Icon_FClose.gif','TreeImage/Icon_FOpen.gif','50033','50033',0);");
//	buffer.append("tree.add(tree50033);");
	 
	buffer.append("document.write(tree);");
	buffer.append("</SCRIPT>");          

	 
			
			return buffer.toString();	
	}	
	
	
	
}
